//
//  MYYLoginCell.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/26.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYYLoginCell.h"
#import "MYY_User.h"


#import "UIImageView+WebCache.h"

#define kTextFieldHeight 50
typedef void(^DeleteUserBlock)(MYY_User *user);

@interface MYYLoginCell()

@property(nonatomic, strong) UIImageView *userAvatar;
@property(nonatomic, strong) UILabel *loginNameLabel;
@property(nonatomic, strong) UIButton *clearButton;
@property(nonatomic, strong) UIView *seperatorView;

@property(nonatomic, strong) MYY_User *user;
@property(nonatomic, copy) DeleteUserBlock deleteUserBlock;

@end

@implementation MYYLoginCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor =MYYColorFromRGB(0xd7f2ef);
        [self.contentView addSubview:self.userAvatar];
        [self.contentView addSubview:self.loginNameLabel];
        [self.contentView addSubview:self.clearButton];
        [self.contentView addSubview:self.seperatorView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.userAvatar.frame = CGRectMake(17, 10, 30, 30);
    if (iphone6 || iphone6P) {
        self.clearButton.frame = CGRectMake(WIDTH - 45, (CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
    }
    else {
        self.clearButton.frame = CGRectMake(WIDTH - 41, (CGRectGetHeight(self.bounds) - 27)/2.0, 27, 27);
    }
    //self.clearButton.frame = CGRectMake(JXTScreenWidth - 19-40, (CGRectGetHeight(self.bounds) - 40)/2.0, 40, 40);
    self.loginNameLabel.frame = CGRectMake(CGRectGetMaxX(self.userAvatar.frame)+14, 0, CGRectGetMinX(self.clearButton.frame) - 10 - CGRectGetMaxX(self.userAvatar.frame)-20, kTextFieldHeight);
    self.seperatorView.frame = CGRectMake(16, CGRectGetHeight(self.bounds)-0.5, WIDTH-32, 0.5);
}

- (void)setUser:(MYY_User *)user deleteUserBlock:(void(^)(MYY_User *user))deleteUserBlock
{
    self.user = user;
    self.deleteUserBlock = deleteUserBlock;
    [self.userAvatar sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"feed_user_default"]];
    self.loginNameLabel.text = user.account;
    [self setNeedsLayout];
}

- (void)DeleteUserTapEvent
{
    self.deleteUserBlock(self.user);
}

#pragma mark - property
- (UIImageView *)userAvatar
{
    if (_userAvatar == nil) {
        _userAvatar = [UIImageView  new];
        _userAvatar.contentMode = UIViewContentModeScaleAspectFit;
        _userAvatar.clipsToBounds = YES;
        _userAvatar.layer.cornerRadius = 15;
    }
    return _userAvatar;
}

- (UILabel *)loginNameLabel
{
    if (_loginNameLabel == nil) {
        _loginNameLabel = [UILabel new];
        _loginNameLabel.font = [UIFont systemFontOfSize:17];
        _loginNameLabel.textColor = [UIColor blackColor];
    }
    return _loginNameLabel;
}

- (UIButton *)clearButton
{
    if (_clearButton == nil) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_clearButton setImage:[UIImage imageNamed:((iphone6P||iphone6)?@"chahaozu":@"chahaozu")] forState:UIControlStateNormal];
        [_clearButton addTarget:self action:@selector(DeleteUserTapEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}

- (UIView *)seperatorView
{
    if (_seperatorView == nil) {
        _seperatorView = [UIView new];
        _seperatorView.backgroundColor = RGBACOLOR(218, 225, 234, 1.0);
    }
    return _seperatorView;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
